import os
import pytest

import static
from common.utils.file_util import FileUtil


def run_testcase(case_dir, class_name=None, method_name=None, like=None):
    if like:
        # 只运行包含关键字like的用例,注：模块中包含关键字，则模块下所有用例都会运行
        pytest.main(['-k', like, '-v', case_dir])
    else:
        if class_name:
            case_dir += '::' + class_name
        if method_name:
            case_dir += '::' + method_name
        pytest.main(['-v', case_dir])


def run_marked_case(case_dir, mark):
    case_name = os.path.split(case_dir)[1]
    pytest.main(['-m', mark, '-v', case_name])


def run_and_generate_report(case_dir, like=None):
    base_dir = static.REPORT_PATH
    json_path = os.path.join(base_dir, 'allure_result')
    FileUtil.mkdir(json_path)
    html_path = os.path.join(base_dir, 'allure_report')
    FileUtil.mkdir(html_path)
    command = ['-v', case_dir, '--alluredir=' + json_path]
    if like:
        command.extend(['-k', like])
    pytest.main(command)
    # 使用allure工具生成测试报告
    os.system(f"allure generate {json_path} -o {html_path} --clean")